package org.example;

import io.netty.buffer.ByteBuf;

import java.util.HashMap;
import java.util.Map;

import static org.example.Command.*;


public class PacketCodec {
    public static final int MAGIC_NUMBER = 0x12345678; // 16进制，4个字节 魔数
    public static final int MAGIC_NUMBER2 = 0x12345688; // 16进制，4个字节 魔数

    private final Map<Byte, Serializer> serializerMap;

    public static final PacketCodec INSTANCE = new PacketCodec();
    private final Map<Byte, Class<? extends Packet>> packetTypeMap;

    private PacketCodec() {
        packetTypeMap = new HashMap<>();
        packetTypeMap.put(LOGIN_REQUEST, LoginRequestPacket.class);
        packetTypeMap.put(LOGIN_RESPONSE, LoginResponsePacket.class);
        packetTypeMap.put(MESSAGE_REQUEST, MessageRequestPacket.class);
        packetTypeMap.put(MESSAGE_RESPONSE, MessageResponsePacket.class);
        packetTypeMap.put(CREATE_GROUP_REQUEST, CreateGroupRequestPacket.class);
        packetTypeMap.put(CREATE_GROUP_RESPONSE, CreateGroupResponsePacket.class);
        packetTypeMap.put(JOIN_GROUP_REQUEST, JoinGroupRequestPacket.class);
        packetTypeMap.put(JOIN_GROUP_RESPONSE, JoinGroupResponsePacket.class);
        packetTypeMap.put(QUIT_GROUP_REQUEST, QuitGroupRequestPacket.class);
        packetTypeMap.put(QUIT_GROUP_RESPONSE, QuitGroupResponsePacket.class);
        packetTypeMap.put(LIST_GROUP_MEMBERS_REQUEST, MemberGroupRequestPacket.class);
        packetTypeMap.put(LIST_GROUP_MEMBERS_RESPONSE, MemberGroupResponsePacket.class);

        serializerMap = new HashMap<>();
        Serializer serializer = new JSONSerializer();
        serializerMap.put(serializer.getSerializerAlgorithm(), serializer);
    }

    // 编码
    public ByteBuf encode(ByteBuf byteBuf, Packet packet){
        // 序列化java对象
        byte[] serialize = Serializer.DEFAULT.serialize(packet);

        // 实际编码到 bytebuf 中
        byteBuf.writeInt(MAGIC_NUMBER); // 4 字节 魔数
        byteBuf.writeByte(packet.version); // 1 字节 版本号
        byteBuf.writeByte(Serializer.DEFAULT.getSerializerAlgorithm()); // 1 字节 序列化算法
        byteBuf.writeByte(packet.getCommand()); // 1 字节 指令
        byteBuf.writeInt(serialize.length); // 4 字节 数据长度
        byteBuf.writeBytes(serialize); // N 字节 数据

        return byteBuf;
    }

    // 解码
    public Packet decode(ByteBuf byteBuf){
        // 跳过魔数
        byteBuf.skipBytes(4);

        // 跳过版本号
        byteBuf.skipBytes(1);

        // 获取序列化算法
        byte serializerAlgorithm = byteBuf.readByte();

        // 获取指令
        byte command = byteBuf.readByte();

        // 数据长度
        int length = byteBuf.readInt();


        Serializer serializer = getSerializer(serializerAlgorithm);
        Class<? extends Packet> requestType = getRequestType(command);

        // 将 bytebuf 中的数据全部读取到指定的目标 bytes 数组中
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);

        if(serializer != null && requestType != null){
            return serializer.deserializer(requestType, bytes);
        }else {
            System.out.println("序列化算法 | IM指令不正确");
        }

        return null;

    }

    private Serializer getSerializer(byte serializerAlgorithm) {
        return serializerMap.get(serializerAlgorithm);
    }

    private Class<? extends Packet> getRequestType(byte command) {
        return packetTypeMap.get(command);
    }
}
